home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap12 / UpDown.java < prev   
Encoding:
Java Source  |  1997-04-20  |  963 b   |  46 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class UpDown extends Applet {
  5.    static int RADIUS = 20;
  6.    static int X = 30;
  7.    public int y = 30;
  8.  
  9.    public void init() {
  10.       new BounceThread(this).start();
  11.    }
  12.  
  13.    public void paint(Graphics g) {
  14.       g.setColor(Color.blue);
  15.       g.drawOval(X - RADIUS, y - RADIUS,
  16.          2 * RADIUS, 2 * RADIUS);
  17.    }
  18. }
  19.  
  20. class BounceThread extends Thread {
  21.    UpDown applet;
  22.    int    yDir = +1;
  23.    int    incr = 10;
  24.    int    sleepFor = 100;
  25.  
  26.    BounceThread(UpDown a) {
  27.       this.applet = a;
  28.    }
  29.  
  30.    public void run() {
  31.       while (true) {
  32.          applet.y += (incr * yDir);
  33.          applet.repaint();
  34.  
  35.          if (applet.y - UpDown.RADIUS < incr ||
  36.                applet.y + UpDown.RADIUS + incr > applet.size().height)
  37.             yDir *= -1;
  38.  
  39.          try {
  40.             sleep(sleepFor);
  41.          } catch (InterruptedException e) {
  42.          }
  43.       }
  44.    }
  45. }
  46.